package interfazeak;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import laguntzaileak.BarietateKop;

import db.BarietateaKud;
import db.DBKudeatzailea;


public class Barietatea extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JLabel lblNewLabel;
	private JLabel ehunekoa;
	private Vector<BarietateKop> barietateKant;
	private JCheckBox check;
	private upeletanSartu gurasoa;
	private Vector<JTextField> ehunekoeremuak;
	private Vector<JCheckBox> barietatecheck;
	private Vector<Integer> ehunekobarietate;
	private BarietateKop bkop;
	private Vector<String> barietateak;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			final Boolean MODALA = true;
			Vector<BarietateKop> bar = new Vector<BarietateKop>();
			Barietatea dialog = new Barietatea(new upeletanSartu(null), MODALA, bar);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 * @param barietateenbektorea 
	 * 
	 * @param barietateenbektorea
	 */
	public Barietatea(final upeletanSartu guraso, boolean modal, Vector<BarietateKop> barietateenbektorea) {
		this.gurasoa = guraso;
		
		ehunekoeremuak = new Vector<JTextField>();
		barietatecheck = new Vector<JCheckBox>();
		barietateKant = new Vector<BarietateKop>();

		setBounds(100, 100, 600, 600);
		setTitle("BARIETATEAK");
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setLayout(new FlowLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);

		lblNewLabel = new JLabel("Ehunekoa");
		contentPanel.add(lblNewLabel);
		lblNewLabel.setBounds(380, 30, 150, 15);
		contentPanel.setLayout(null);
		beteBarietateak(barietateenbektorea);

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
		getContentPane().add(buttonPane, BorderLayout.SOUTH);

		JButton okButton = new JButton("GORDE");

		okButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				// ehuneko guztien batura ehun baino gutxiago eman behar du
				int emaitza = Barietatea.this.lortuehunekoak();
				if (emaitza==-1){
					Barietatea.this.dispose();
				}
				else if (emaitza != 100) {

					JOptionPane.showMessageDialog(Barietatea.this,
							"%100 bete behar dute." + "Ez da gordeko", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
					
			
					Barietatea.this.dispose();
				}
				
				else if (emaitza == 100) {
					Barietatea.this.eguneratu();
					Barietatea.this.gurasoa.gordeBarietatea(barietateKant);
					Barietatea.this.dispose();
				} 
			}
		});
		okButton.setActionCommand("GORDE");
		buttonPane.add(okButton);
		// getRootPane().setDefaultButton(okButton); defektuz aukeratuta
	}

	

	protected void eguneratu() {
		int i = 1;

		while (ehunekobarietate.size() >= i) {
			bkop = new BarietateKop(0, 0);
			bkop.setEhunekoa(ehunekobarietate.elementAt(i - 1));
			int id = BarietateaKud.lortuId(barietateak.elementAt(i-1));
			bkop.setBarietateID(id);
			barietateKant.add(i - 1, bkop);

			i = i + 1;
		}

	}

	public int lortuehunekoak() {
		int i = 1;
		int emaitza = 0;

		boolean urten=false;
		while (ehunekoeremuak.size() >= i && urten==false) {

			

			JTextField eremua = ehunekoeremuak.elementAt(i - 1);
			if /* ((barietatecheck.elementAt(i - 1).isSelected()) && */(ehunekoeremuak
					.elementAt(i - 1).isEnabled()) {
				String e = eremua.getText();
				if (e.equals("")){
					JOptionPane.showMessageDialog(Barietatea.this,
							"Aukeratutako barietateen ehunekoak bete behar dira!!!", "KONTUZ",
							JOptionPane.WARNING_MESSAGE);
					urten =true;
				}
				else {
					try {
						
						Integer ehu = Integer.parseInt(e);
				ehunekobarietate.add(i - 1, ehu);
				emaitza = emaitza + ehu;
						
					}
					catch (NumberFormatException ex){
						JOptionPane.showMessageDialog(Barietatea.this,
								"ZENBAKIAK sartu behar dira!!!", "KONTUZ",
								JOptionPane.WARNING_MESSAGE);
						urten =true;
					}
					
				}
				

			} else {

				ehunekobarietate.add(i - 1, 0);
			}

			i = i + 1;
		}
		
		if (urten==false){
			return emaitza;
		}
		else {
			emaitza=-1;
			ehunekobarietate.removeAllElements();
			return emaitza;
		}
	}

	private void beteBarietateak(Vector<BarietateKop> barietateenbektorea) {
		DBKudeatzailea dbk = DBKudeatzailea.getInstantzia();
		String k1 = " SELECT id, izena FROM Barietatea";
		ehunekobarietate = new Vector<Integer>();
		barietateak = new Vector<String>();
		ResultSet rs = dbk.execSQL(k1);
		int x = 76, y = 50, h = 200, w = 23, i = 0;

		try {

			while (rs.next()) {
				String barietatea = rs.getString("izena");
				barietateak.add(i,barietatea);
				int id = rs.getInt("id");

				textField = new JTextField();

				textField.setEnabled(false);
				textField.setBounds(380, y, 45, 19);
				contentPanel.add(textField);
				textField.setColumns(10);
				ehunekoeremuak.add(textField);

				// ehunekobarietate.add(0);

				check = new JCheckBox(barietatea);
				check.setBounds(x, y, h, w);
				// check.setActionCommand(barietatea);
				contentPanel.add(check);
				
				

				if (barietateenbektorea.size()>0){
					int idbar =  BarietateaKud.lortuId(barietatea); // kontuz
						if (barietateenbektorea.elementAt(i).getEhunekoa()>0){
							check.setSelected(true);
							String ehunekoa = Integer.toString(barietateenbektorea.elementAt(i).getEhunekoa());;
							textField.setEnabled(true);
							textField.setText(ehunekoa);
							
							check.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent e) {

									JCheckBox zebarietate = (JCheckBox) e.getSource();
									// bilatu barietatearen posizioa bektorean
									final int posizioa = barietatecheck
											.indexOf(zebarietate);
									// barietateari dagokion ehunekoa lortu.
									JTextField ehuneko = ehunekoeremuak.elementAt(posizioa);
									// gaitu (edo desgaitu) ehuneko eremua
									ehuneko.setEnabled(zebarietate.isSelected());
									

								}
							});
						}
						else {
							check.setSelected(false);
							textField.setEnabled(false);
							check.addActionListener(new ActionListener() {
								public void actionPerformed(ActionEvent e) {

									System.out.println("banago");
									JCheckBox zebarietate = (JCheckBox) e.getSource();
									// bilatu barietatearen posizioa bektorean
									final int posizioa = barietatecheck
											.indexOf(zebarietate);
									// barietateari dagokion ehunekoa lortu.
									JTextField ehuneko = ehunekoeremuak.elementAt(posizioa);
									// gaitu (edo desgaitu) ehuneko eremua
									ehuneko.setEnabled(zebarietate.isSelected());
									

								}
							});
						}
					}
				else {
					check.addActionListener(new ActionListener() {
						public void actionPerformed(ActionEvent e) {

							// Barietatea gehitzerakoan dagokion ehunekoa gaitu

							// ze barietetatean egin dute klik?
							JCheckBox zebarietate = (JCheckBox) e.getSource();
							// bilatu barietatearen posizioa bektorean
							final int posizioa = barietatecheck
									.indexOf(zebarietate);
							// barietateari dagokion ehunekoa lortu.
							JTextField ehuneko = ehunekoeremuak.elementAt(posizioa);
							// gaitu (edo desgaitu) ehuneko eremua
							ehuneko.setEnabled(zebarietate.isSelected());
							
							
						}
					});
				}
				barietatecheck.add(check);
				ehunekoa = new JLabel("%");
				ehunekoa.setBounds(330, y, 30, 15);
				contentPanel.add(ehunekoa);

				y = y + 70;
				i=i+1;

			}

		} catch (SQLException e) {

			e.printStackTrace();
		}

	}

	

	

}